package com.telenav.network;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.URLConnection;

public class NetworkManager {
	
    public static final int READ = 0;
    
    public static final int READ_WRITE = 1;
    
    public static final int WRITE = 2;
    
    private static NetworkManager networkManager;
    
    public synchronized static NetworkManager getInstance()
    {
        if(networkManager == null)
        {
        	networkManager =  new NetworkManager();
        }
    	return networkManager;
    }
    
    public NetworkConnection openConnection(String name) throws IOException
    {
        return openConnection(name, READ_WRITE);
    }

    public NetworkConnection openConnection(String name, int mode) throws IOException
    {
        return openConnection(name, mode, false);
    }
   
    public NetworkConnection openConnection(String name, int mode, boolean timeout) throws IOException
    {
    	if (name == null || name.trim().length() == 0)
        {
            throw new IllegalArgumentException("The url is empty.");
        }
        if (name.startsWith("socket://"))
        {
            try
            {
                int lastIndex = name.lastIndexOf(":");
                String dstName = name.substring("socket://".length(), lastIndex);
                int dstPort = Integer.parseInt(name.substring(lastIndex + 1).trim());
                InetSocketAddress address =  new InetSocketAddress(dstName, dstPort);
                return new SocketConnection(address, timeout);
            }
            catch(Exception e)
            {
                throw new IOException("Error on make socket request!");
            }
        }
        else
        {
            try
            {
                name = name.replaceAll(" ", "%20");
                URL url = new URL(name);
                URLConnection urlConnection = null;
                urlConnection = url.openConnection();
                if (urlConnection instanceof HttpURLConnection)
                {
                    HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
                    if (mode == READ)
                    {
                        httpUrlConnection.setDoInput(true);
                    }
                    else if (mode == WRITE)
                    {
                        httpUrlConnection.setDoOutput(true);
                    }
                    else
                    {
                        httpUrlConnection.setDoInput(true);
                        httpUrlConnection.setDoOutput(true);
                    }

                    if (timeout)
                    {
                        httpUrlConnection.setConnectTimeout(30 * 1000);
                        httpUrlConnection.setReadTimeout(30 * 1000);
                    }

                    return new HttpConnection(httpUrlConnection);
                }
                else
                {
                    throw new IOException("Currently don't support this protocol.");
                }
            }
            catch(Exception e)
            {
            	throw new IOException("Error on make http request!");
            }
        }
    }
}
